package l4;
import java.util.Scanner;

public class test {
    private static Scanner scanner = new Scanner(System.in);
    private static product<Integer> list = new product<>();
    public static void main(String[] args) {
        System.out.println("欢迎使用农产品管理系统！");
        showMenu();
    }

    private static void showMenu() {
        System.out.println("\n请选择操作：");
        System.out.println("0. 退出程序");
        System.out.println("1. 插入");
        System.out.println("2. 删除");
        System.out.println("3. 查找元素");
        System.out.println("4. 获取指定位置的元素");
        System.out.println("5. 获取大小");
        System.out.println("6. 判断集合是否为空");
        System.out.println("7. 遍历集合");

        while (true) {
            System.out.print("\n请输入操作编号: ");
            int operation = scanner.nextInt();

            switch (operation) {
                case 0:
                    System.out.println("程序退出！");
                    System.exit(0);
                    break;
                case 1:
                    System.out.print("请输入插入位置: ");
                    int index = scanner.nextInt();
                    System.out.print("请输入插入的元素: ");
                    int value = scanner.nextInt();
                    try {
                        list.insert(index, value);
                        System.out.println("元素插入成功！");

                    }catch (Exception e) {
                        System.out.println("插入失败: " + e.getMessage());
                    }
                    break;
                case 2:
                    System.out.print("请输入要删除的元素位置: ");
                    index = scanner.nextInt();
                    try {
                        list.remove(index);
                        System.out.println("元素删除成功！");
                    }catch (Exception e){
                        System.out.println("删除失败: " + e.getMessage());
                    }
                    break;
                case 3:
                    System.out.print("请输入要查找的元素: ");
                    value = scanner.nextInt();
                    int searchIndex = list.search(value);
                    if (searchIndex != -1) {
                        System.out.println("元素第一次出现的位置为: " + searchIndex);
                    } else {
                        System.out.println("未找到该元素！");
                    }
                    break;
                case 4:
                    System.out.print("请输入要查找的元素位置: ");
                    index = scanner.nextInt();
                    try {
                        System.out.println("元素为: " + list.visit(index));
                    }catch (Exception e){
                        System.out.println("获取失败: " + e.getMessage());
                    }
                    break;
                case 5:
                    System.out.println("集合的大小为: " + list.size());
                    break;
                case 6:
                    System.out.println("集合是否为空: " + list.isEmpty());
                    break;
                case 7:
                    System.out.println("集合元素为: ");
                    list.traverse();
                    break;

                default:
                    System.out.println("无效选项，请重新输入！");
                    break;
            }
        }
    }
}

